<template>
	<view class="wrap">
		<view class="top"></view>
		<view class="content">
			<view class="title">欢迎登录</view>
		</view>
		<view class="buttom">
			<view class="loginType">
				<!-- #ifdef MP-WEIXIN -->
				<button type="primary" size="default" class="login-btn" open-type="getPhoneNumber" @getphonenumber="loginForWechatMini">
					手机号快捷登录
				</button>
				<!-- #endif -->
			</view>
		</view>
		<u-toast ref="uToast"></u-toast>
		
    <view class="tech-support">
      技术支持:芜湖市蛋壳信息科技有限公司
    </view>
	</view>
</template>

<script>
import { mapMutations, mapState } from 'vuex';
export default {
	data() {
		return {
			openid: ''
		};
	},
	computed:{
		...mapState(['openid'])
	},
	methods: {
		...mapMutations(['SET_MEMBER']),
		// #ifdef MP-WEIXIN
		async loginForWechatMini(e) {
			try {
				// 先获取新鲜的 code
				const loginRes = await new Promise((resolve, reject) => {
					uni.login({
						provider: 'weixin',
						success: resolve,
						fail: reject
					});
				});

				if (!loginRes.code) {
					throw new Error('获取 code 失败: ' + loginRes.errMsg);
				}

				// 原微信登录方式（保留注释）
				// let res = await this.$u.api.userLoginForWechatMini({
				// 	encryptedData: e.detail.encryptedData,
				// 	iv: e.detail.iv,
				// 	openid: this.openid
				// });
				
				// 使用userAuthSession接口登录（使用新获取的 code）
				let res = await this.$u.api.userAuthSession({
					code: loginRes.code,
					// encryptedData: e.detail.encryptedData, // 如果后端需要，取消注释
					// iv: e.detail.iv, // 如果后端需要，取消注释
					// openid: this.openid // 如果后端需要，取消注释
				});
				
				if (res) {
					this.SET_MEMBER(res.userInfo);
					
					this.$refs.uToast.show({
						title: '登录成功',
						type: 'success'
					});

					setTimeout(() => {
						uni.navigateBack();
					}, 2000);
				} else {
					this.$refs.uToast.show({
						title: '登录失败，请重试',
						type: 'error'
					});
				}
			} catch (error) {
				console.error('登录错误:', error);
				this.$refs.uToast.show({
					title: '登录失败: ' + (error.message || '未知错误'),
					type: 'error'
				});
			}
		}
		// #endif
	}
};
</script>

<style lang="scss" scoped>
.wrap {
	background-color: #ffffff;
	font-size: 28rpx;
	height: 100%;
	
	.content {
		width: 600rpx;
		margin: 0 auto;
		padding-top: 100rpx;

		.title {
			text-align: center;
			font-size: 60rpx;
			font-weight: 500;
			margin-bottom: 100rpx;
		}
	}

	.buttom {
		.loginType {
			padding: 80rpx;
			
			.login-btn {
				width: 100%;
				border-radius: 50rem !important;
				align-items: center;
				justify-content: center;
				padding: 10rpx 0;
				text-align: center;
			}
		}
	}
}
</style>


<style lang="scss" scoped>
.wrap {
	background-color: #ffffff;
	font-size: 28rpx;
	height: 100%;
	
	.content {
		width: 600rpx;
		margin: 0 auto;
		padding-top: 100rpx;

		.title {
			text-align: center;
			font-size: 60rpx;
			font-weight: 500;
			margin-bottom: 100rpx;
		}
	}

	.buttom {
		.loginType {
			padding: 80rpx;
			
			.login-btn {
				width: 100%;
				border-radius: 50rem !important;
				align-items: center;
				justify-content: center;
				padding: 10rpx 0;
				text-align: center;
			}
		}
	}
}

.tech-support {
  text-align: center;
  padding: 30rpx 0;
  color: #9A9C9B;
  font-size: 24rpx;
  margin-top: 20rpx;
  position: absolute;
  left: 0;
  right: 0;
  z-index: 1;
  bottom: 0;
}
</style>
